package cn.gjculture.shopping.api.controller;

import cn.gjculture.shopping.api.entity.*;
import cn.gjculture.shopping.api.service.HotelService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping(value = "/hotel")
public class HotelController {

    @Autowired
    HotelService hotelService;


    @ApiOperation(value = "新增酒店", notes = "新增酒店")
    @ApiImplicitParam(name = "hotel", value = "酒店对象", required = true, dataType = "Object")
    @PostMapping
    public Return save(String param) {
        ParamData<Hotel> g = JSONObject.parseObject(param, new TypeReference<ParamData<Hotel>>() {});
        Return ret = new Return();
        if (hotelService.insert(g.getData()) > 0) {
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value="修改酒店", notes="修改酒店")
    @ApiImplicitParam(name = "hotel", value = "酒店对象", required = true, dataType = "Object")
    @PutMapping
    public Return update(String param){
        ParamData<Hotel> g = JSONObject.parseObject(param, new TypeReference<ParamData<Hotel>>() {});
        Return ret=new Return();
        if( hotelService.update(g.getData())>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value = "获取酒店列表", notes = "获取酒店列表")
    @GetMapping
    public Return<List<Hotel>> query(PageInfo pageInfo) {
        Return<List<Hotel>> ret = new Return<>();
        List<Hotel> hotels = hotelService.query();
        ret.setData(hotels);
        ret.getCommon().setRet(true);
        return ret;
    }

    @ApiOperation(value = "获取酒店", notes = "获取酒店")
    @ApiImplicitParam(name = "hotelId", value = "酒店ID", required = true, dataType = "String",paramType = "path")
    @GetMapping(value = "/{hotelId}")
    public Return<Hotel> getHotelById(@PathVariable(value = "hotelId") String hotelId) {
        Return<Hotel> ret = new Return<>();
        Hotel hotel = hotelService.queryById(hotelId);
        ret.setData(hotel);
        return ret;
    }


    @ApiOperation(value = "删除酒店", notes = "删除酒店")
    @ApiImplicitParam(name = "hotelIds", value = "酒店ID", required = true, dataType = "String",paramType = "path")
    @DeleteMapping(value = "/{hotelIds}")
    public Return<Hotel> delete(@PathVariable(value = "hotelIds") List<String> hotelIds) {
        Return<Hotel> ret = new Return<>();
        if (hotelService.delete(hotelIds,ret) > 0) {
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value="修改推荐酒店", notes="根据酒店ID修改推荐酒店")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hotelId", value = "酒店id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "recommended", value = "是否推荐,1:是不推荐，0:是推荐", required = true, dataType = "Integer" )
    })
    @PutMapping("/recommended")
    public Return recommended(String hotelId,Integer recommended, Return ret){
        Hotel hotel=new Hotel();
        hotel.setHotelId(hotelId);
        hotel.setRecommended(recommended);
        if(hotelService.recommended(hotel)>0)
            ret.getCommon().setRet(true);
        return ret;
    }
    @ApiOperation(value = "获取推荐商品")
    @GetMapping(value = "/recommended")
    public Return<List<Hotel>> queryRecommended(PageInfo pageInfo, Return<List<Hotel>> ret){
        List<Hotel> goods=hotelService.queryRecommended();
        ret.setData(goods);
        return ret;
    }


}
